package com.aracky.system.service.impl;

import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.PageObjectVO;
import com.aracky.common.vo.PageVO;
import com.aracky.system.mapper.SysMenuTableMapper;
import com.aracky.system.model.entity.SysMenuTable;
import com.aracky.system.model.form.SysMenuTableForms;
import com.aracky.system.model.request.SysMenuTableRequest;
import com.aracky.system.model.vo.SysMenuTableListVO;
import com.aracky.system.model.vo.SysMenuTableVO;
import com.aracky.system.service.SysMenuTableService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单和数据模型配置关系表 服务实现类
 * </p>
 *
 * @author zengzhiping
 * @since 2022-07-19 10:22:14
 */
@Service
public class SysMenuTableServiceImpl extends ServiceImpl<SysMenuTableMapper, SysMenuTable> implements SysMenuTableService {
    @Resource
    private SysMenuTableMapper sysMenuTableMapper;

    /**
     * 保存数据和修改数据接口
     *
     * @param sysTableHeaderForms
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateSysMenuTable(SysMenuTableForms sysTableHeaderForms) {
        List<SysMenuTable> sysMenuTableList = ConvertUtil.sourceToTargets(sysTableHeaderForms.getSysMenuTableFormList(), SysMenuTable.class);
        List<SysMenuTable> sysMenuTableList1 = new ArrayList<>();
        for (SysMenuTable sysMenuTable : sysMenuTableList) {
            sysMenuTable.setMenuId(sysTableHeaderForms.getMenuId());
            sysMenuTableList1.add(sysMenuTable);
        }
        this.saveOrUpdateBatch(sysMenuTableList1);
    }

    /**
     * 删除数据
     *
     * @param mtIds
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSysMenuTable(List<Long> mtIds) {
        this.removeBatchByIds(mtIds);
    }

    /**
     * 查询分页数据
     *
     * @param sysMenuTableRequest
     * @return
     */
    @Override
    public PageObjectVO<SysMenuTableVO> getMenuTablePage(SysMenuTableRequest sysMenuTableRequest) {
        IPage<SysMenuTableListVO> page = sysMenuTableMapper.getMenuTable(sysMenuTableRequest.toPage(), sysMenuTableRequest);

        PageObjectVO<SysMenuTableVO> pageObjectVO = new PageObjectVO<>();

        pageObjectVO.setPage(page.getCurrent());
        pageObjectVO.setSize(page.getSize());
        pageObjectVO.setTotalCount(page.getTotal());
        pageObjectVO.setTotalPage(page.getPages());

        SysMenuTableVO sysMenuTableVO = new SysMenuTableVO();
        sysMenuTableVO.setMenuId(sysMenuTableRequest.getMenuId());
        sysMenuTableVO.setSysMenuTableList( page.getRecords());

        pageObjectVO.setData(sysMenuTableVO);


        return pageObjectVO;
    }
}
